/**
 * JMPlurk, a Java Mobile client for Plurk
 * Copyright (C) 2009 Petra Barus
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.maleskoding.jmplurk;

import com.maleskoding.jmplurk.plurkapi.PlurkAPI;
import com.maleskoding.jmplurk.plurkapi.PlurkException;
import com.maleskoding.jmplurk.ui.MainPlurkForm;
import com.maleskoding.jmplurk.ui.NewPlurkForm;
import com.maleskoding.jmplurk.ui.SettingForm;
import com.maleskoding.jmplurk.util.Preferences;
import com.sun.lwuit.Display;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
import java.io.IOException;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStoreException;

/**
 * @author Petra Barus
 */
public class JMPlurk extends MIDlet {

    //
    public static final String RECORDSTORE_ID = "JMPlurk";
    //
    private boolean midletPaused = false;
    private boolean isFirstTime = true;
    public static PlurkAPI plurk = new PlurkAPI();
    //
    //<editor-fold defaultstate="collapsed" desc="UI Objects">
    private SettingForm settingForm;
    private MainPlurkForm mainPlurkForm;
    private NewPlurkForm newPlurkForm;
    //</editor-fold>

    /**
     * 
     */
    public void startApp() {
        try {
            //deleteRecords();
            System.err.println("Starting App!");
            Display.init(this);
            Resources r1 = Resources.open("/jmplurk.res");
            UIManager.getInstance().setThemeProps(r1.getTheme(r1.getThemeResourceNames()[0]));
            Preferences p = Preferences.getInstance();
            if (p.get("username") == null || p.get("username").length() == 0
                    || p.get("password") == null || p.get("password").length() == 0) {
                System.err.println("First time");
                getSettingForm().show();
            } else {
                System.err.println("Not first time");
                getMainPlurkForm().show();
            }
        } catch (RecordStoreException ex) {
            System.err.println("Not first time");
            getSettingForm().show();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     *
     */
    public void pauseApp() {
    }

    /**
     * 
     * @param unconditional
     */
    public void destroyApp(boolean unconditional) {
    }

    /**
     * 
     */
    public void showTimeOutAlert() {
    }

    //<editor-fold defaultstate="expanded" desc="Forms">
    //<editor-fold defaultstate="expanded" desc="Forms Main Display">
    public SettingForm getSettingForm() {
        if (settingForm == null) {
            settingForm = new SettingForm(this);
        }
        return settingForm;
    }

    //</editor-fold>
    //
    //<editor-fold defaultstate="collapsed" desc="Form Main">
    public MainPlurkForm getMainPlurkForm() {
        if (mainPlurkForm == null) {
            mainPlurkForm = new MainPlurkForm(this);
        }
        return mainPlurkForm;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Form New Plurk">
    public NewPlurkForm getNewPlurkForm() {
        if (newPlurkForm == null) {
            newPlurkForm = new NewPlurkForm(this);
        }
        return newPlurkForm;
    }
    //</editor-fold>
    //</editor-fold>
    //
    //<editor-fold defaultstate="collapsed" desc="Control methods">

    /**
     * 
     */
    public void exitMIDlet() {
        destroyApp(true);
        notifyDestroyed();
    }
//</editor-fold>
}
